<?php
/**
 * 二分查找  
 * @parame  array  从小到大排列的数组
 * @parame  search 要查找的数字
 * @author  yajun
 */
function erfen($arr,$search)
{
	if(count($arr) <= 1)
	{
		if($arr[0] == $search);
		{
			return 0;
		}
		return false;
	}
	$leftsize = 0;
	$rightsize = count($arr)-1;
	while(true)
	{
		$left = $arr[$leftsize];
		$right = $arr[$rightsize];
		$midsize  = ceil(($rightsize-$leftsize)/2);
		$mid = $arr[$midsize];
		if($search < $mid)
		{
			if($search < $left)
			{
				return false;
			}
			elseif($search == $left)
			{
				return $leftsize;
			}
			$rightsize = $midsize;
			continue;
		}
		elseif($search == $mid) 
		{
			return $midsize;
		}
		else
		{
			if($search > $right)
			{
				return false;
			}
			elseif($search == $right)
			{
				return $rightsize;
			}
			$leftsize = $midsize;
			continue;
		}
	}
	return false;		
}
var_dump(erfen(array(-5,-7,-1,1,3,5,7,9,152,11436,51231),5));